package org.boulderfoodrescue.webapp.server.pickups;

import java.util.List;
import java.util.logging.Logger;

import org.boulderfoodrescue.webapp.client.rpc.PickupService;
import org.boulderfoodrescue.webapp.server.users.UserAccountsDatabase;
import org.boulderfoodrescue.webapp.shared.Pickup;
import org.boulderfoodrescue.webapp.shared.UserAccount;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class PickupServiceImpl extends RemoteServiceServlet implements PickupService {
	private static final Logger log = Logger.getLogger(PickupServiceImpl.class.getName());
	
	@Override
	public Long savePickup(Pickup pickup) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute savePickup() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		if(pickup.getDatabaseKey() != null && pickup.getDatabaseKey() != 0){
			return PickupDatabase.updatePickup(pickup); 
		}
		return PickupDatabase.addPickup(pickup);
	}

	@Override
	public List<Pickup> getPickups() {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute getPickups() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		return PickupDatabase.getPickups(1000);
	}

	@Override
	public Boolean removePickup(Pickup pickup) {
		UserService userService = UserServiceFactory.getUserService();
		User userS = userService.getCurrentUser();
		if(userS == null){
			log.warning("User is not logged in. Cannot execute getListUsers() call");
			return false;
		}
		UserAccount userA = UserAccountsDatabase.getUser(userS.getEmail());
		if(userA.getAccessLevel().compareTo(AccessLevel.Admin) < 0 && userS.getEmail() != userA.getEmail())
		{
			log.warning("User does not have authority to get a list of users "+userA.getEmail() +" access level="+userA.getAccessLevel());
			return false;
		}
		PickupDatabase.removePickup(pickup);
		return true;
	}

	@Override
	public Boolean removePickups(List<Pickup> pickups) {
		for(Pickup p : pickups){
			if(!removePickup(p))
				return false;
		}
		return true;
	}

}
